// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import Router from 'vue-router'
import routes from './router'
import Echarts from 'echarts' // 引入echarts

import Vuex from 'vuex'
import storeData from './store'
import storage from 'store'

import { api } from '@/api/request'
import App from './App'
import PreviewImage from '@/components/PreviewImage'

// import vueReqwest from 'vue-reqwest'

import {
  Row, Col, Button, ConfigProvider, FormModel, Icon, Card, message, Notification, Input, Modal, Alert, Checkbox, InputNumber,
  Form, Table, Select, TreeSelect, DatePicker, Pagination, Breadcrumb, Upload, Radio, Tabs, Tooltip, Steps, Badge,
  List, AutoComplete, Descriptions, Cascader, Popconfirm, Carousel, Avatar, Tag, Spin, Empty, Popover, Dropdown, Menu
} from 'ant-design-vue'

import Viewer from 'v-viewer'

import UdIcon from '@/svg'

// eslint-disable-next-line camelcase
import zh_CN from 'ant-design-vue/lib/locale-provider/zh_CN'
import nprogress from 'nprogress'
import VueClipboard from 'vue-clipboard2'

Vue.config.productionTip = false
Vue.prototype.$api = api // 请求接口 api( uri, data)

Vue.component(Row.name, Row)
Vue.component(Col.name, Col)
Vue.component(Button.name, Button)
Vue.component(Icon.name, Icon)
Vue.component(Card.name, Card)
Vue.component(Pagination.name, Pagination)
Vue.component(ConfigProvider.name, ConfigProvider)
Vue.component('preview-img-modal', PreviewImage)
Vue.component(UdIcon.name, UdIcon)
Vue.component(Popover.name, Popover)

Vue.use(VueClipboard)
Vue.use(Input)
Vue.use(Button)
Vue.use(FormModel)
Vue.use(Form)
Vue.use(Table)
Vue.use(Select)
Vue.use(TreeSelect)
Vue.use(DatePicker)
Vue.use(Breadcrumb)
Vue.use(Upload)
Vue.use(Radio)
Vue.use(Tabs)
Vue.use(Tooltip)
Vue.use(Steps)
Vue.use(Badge)
Vue.use(Alert)
Vue.use(Checkbox)
Vue.use(List)
Vue.use(AutoComplete)
Vue.use(Descriptions)
Vue.use(Cascader)
Vue.use(Popconfirm)
Vue.use(Carousel)
Vue.use(Avatar)
Vue.use(Tag)
Vue.use(Dropdown)
Vue.use(Menu)
// Vue.use(vueReqwest)
Vue.use(Viewer)
Vue.use(InputNumber)
Vue.use(Spin)
Vue.use(Empty)

Vue.use(Modal)
Vue.prototype.$confirm = Modal.confirm

Vue.use(message)
Vue.prototype.$message = message

Notification.config({ 'duration': 3 })
Vue.use(Notification)
Vue.prototype.$notification = Notification
Vue.prototype.$echarts = Echarts// 把echarts绑定在vue的原型上 这样在组件里面就可以通过this.$echarts来访问了

Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  const a = originalPush.call(this, location)
  if (a) return a.catch(err => err)
}
Vue.use(Vuex)

Vue.use(nprogress)
Vue.use(Tag)

// 按钮，防止暴力重复点击
Vue.directive('preventReClick', {
  inserted (el, binding) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, binding.value || 3000)
      }
    })
  }
})

Vue.prototype.listAssign = (arra, arrb) => Object.keys(arra).forEach(key => {
  if (arrb) arra[key] = (arrb[key] || arrb[key] === 0) ? arrb[key] : arra[key]
})

const router = new Router(routes)
const store = new Vuex.Store(storeData)

// router.beforeEach((to, from, next) => {
//   if (!storage.get('token')) {
//     next({ path: '/user/login' })
//   }
// })
router.beforeEach((to, from, next) => {
  if (storage.get('token')) {
    next()
  } else {
    if (to.path !== '/user/login') {
      next({ path: '/user/login' })
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  components: { App },
  data () {
    return { zh_CN }
  },
  template: '<a-config-provider :locale="zh_CN"><App/></a-config-provider>'
}).$mount('#app')
